﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess;
using System.Collections;
using EntityLibrary;

namespace EntityDaoLibrary
{
    public class YBYPInfoDAO : IEntityDAO
    {
        private IDataAccess data;
        public YBYPInfoDAO()
        {
        }
        public YBYPInfoDAO(IDataAccess data)
        {
            this.data = data;
        }


        #region IEntityDAO 成员

        public void insertEntity(DataSet entityData)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                ParamField[] pars;
                sqlStruct.SqlString = "INSERT INTO YB_YP_INFO (GG,JX,PRE0,PRE1,PRE2,PRE3,PRE4,PRE5,PRE6,PRE7,PRE8,PRE9,PRICE,PYZJM,REMARK,SFLB,UNIT,WBZJM,YPBM,YPDJ,YPMC) values (@GG,@JX,@PRE0,@PRE1,@PRE2,@PRE3,@PRE4,@PRE5,@PRE6,@PRE7,@PRE8,@PRE9,@PRICE,@PYZJM,@REMARK,@SFLB,@UNIT,@WBZJM,@YPBM,@YPDJ,@YPMC)";
                pars = new ParamField[21];
                pars[0] = new ParamField("@GG", "GG");
                pars[1] = new ParamField("@JX", "JX");
                pars[2] = new ParamField("@PRE0", "PRE0");
                pars[3] = new ParamField("@PRE1", "PRE1");
                pars[4] = new ParamField("@PRE2", "PRE2");
                pars[5] = new ParamField("@PRE3", "PRE3");
                pars[6] = new ParamField("@PRE4", "PRE4");
                pars[7] = new ParamField("@PRE5", "PRE5");
                pars[8] = new ParamField("@PRE6", "PRE6");
                pars[9] = new ParamField("@PRE7", "PRE7");
                pars[10] = new ParamField("@PRE8", "PRE8");
                pars[11] = new ParamField("@PRE9", "PRE9");
                pars[12] = new ParamField("@PRICE", "PRICE");
                pars[13] = new ParamField("@PYZJM", "PYZJM");
                pars[14] = new ParamField("@REMARK", "REMARK");
                pars[15] = new ParamField("@SFLB", "SFLB");
                pars[16] = new ParamField("@UNIT", "UNIT");
                pars[17] = new ParamField("@WBZJM", "WBZJM");
                pars[18] = new ParamField("@YPBM", "YPBM");
                pars[19] = new ParamField("@YPDJ", "YPDJ");
                pars[20] = new ParamField("@YPMC", "YPMC");
                sqlStruct.ParamFields = pars;
                foreach (DataRow row in entityData.Tables["YB_YP_INFO"].Rows)
                {
                    data.ExecuteNonQuery(row, sqlStruct);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        public void updateEntity(DataSet entityData)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                ParamField[] pars;
                foreach (DataRow row in entityData.GetChanges().Tables["YB_YP_INFO"].Rows)
                {
                    switch (row.RowState)
                    {
                        case DataRowState.Added:
                            sqlStruct.SqlString = "INSERT INTO YB_YP_INFO (GG,JX,PRE0,PRE1,PRE2,PRE3,PRE4,PRE5,PRE6,PRE7,PRE8,PRE9,PRICE,PYZJM,REMARK,SFLB,UNIT,WBZJM,YPBM,YPDJ,YPMC) values (@GG,@JX,@PRE0,@PRE1,@PRE2,@PRE3,@PRE4,@PRE5,@PRE6,@PRE7,@PRE8,@PRE9,@PRICE,@PYZJM,@REMARK,@SFLB,@UNIT,@WBZJM,@YPBM,@YPDJ,@YPMC)";
                            pars = new ParamField[21];
                            pars[0] = new ParamField("@GG", "GG");
                            pars[1] = new ParamField("@JX", "JX");
                            pars[2] = new ParamField("@PRE0", "PRE0");
                            pars[3] = new ParamField("@PRE1", "PRE1");
                            pars[4] = new ParamField("@PRE2", "PRE2");
                            pars[5] = new ParamField("@PRE3", "PRE3");
                            pars[6] = new ParamField("@PRE4", "PRE4");
                            pars[7] = new ParamField("@PRE5", "PRE5");
                            pars[8] = new ParamField("@PRE6", "PRE6");
                            pars[9] = new ParamField("@PRE7", "PRE7");
                            pars[10] = new ParamField("@PRE8", "PRE8");
                            pars[11] = new ParamField("@PRE9", "PRE9");
                            pars[12] = new ParamField("@PRICE", "PRICE");
                            pars[13] = new ParamField("@PYZJM", "PYZJM");
                            pars[14] = new ParamField("@REMARK", "REMARK");
                            pars[15] = new ParamField("@SFLB", "SFLB");
                            pars[16] = new ParamField("@UNIT", "UNIT");
                            pars[17] = new ParamField("@WBZJM", "WBZJM");
                            pars[18] = new ParamField("@YPBM", "YPBM");
                            pars[19] = new ParamField("@YPDJ", "YPDJ");
                            pars[20] = new ParamField("@YPMC", "YPMC");
                            sqlStruct.ParamFields = pars;
                            data.ExecuteNonQuery(row, sqlStruct);
                            break;
                        case DataRowState.Deleted:
                            sqlStruct.SqlString = "DELETE YB_YP_INFO WHERE YPBM=@YPBM";
                            pars = new ParamField[1];
                            pars[0] = new ParamField("@YPBM", "YPBM");
                            sqlStruct.ParamFields = pars;
                            row.RejectChanges();
                            data.ExecuteNonQuery(row, sqlStruct);
                            break;
                        case DataRowState.Modified:
                            sqlStruct.SqlString = "UPDATE YB_YP_INFO SET GG=@GG,JX=@JX,PRE0=@PRE0,PRE1=@PRE1,PRE2=@PRE2,PRE3=@PRE3,PRE4=@PRE4,PRE5=@PRE5,PRE6=@PRE6,PRE7=@PRE7,PRE8=@PRE8,PRE9=@PRE9,PRICE=@PRICE,PYZJM=@PYZJM,REMARK=@REMARK,SFLB=@SFLB,UNIT=@UNIT,WBZJM=@WBZJM,YPDJ=@YPDJ,YPMC=@YPMC WHERE YPBM=@YPBM";
                            pars = new ParamField[21];
                            pars[0] = new ParamField("@GG", "GG");
                            pars[1] = new ParamField("@JX", "JX");
                            pars[2] = new ParamField("@PRE0", "PRE0");
                            pars[3] = new ParamField("@PRE1", "PRE1");
                            pars[4] = new ParamField("@PRE2", "PRE2");
                            pars[5] = new ParamField("@PRE3", "PRE3");
                            pars[6] = new ParamField("@PRE4", "PRE4");
                            pars[7] = new ParamField("@PRE5", "PRE5");
                            pars[8] = new ParamField("@PRE6", "PRE6");
                            pars[9] = new ParamField("@PRE7", "PRE7");
                            pars[10] = new ParamField("@PRE8", "PRE8");
                            pars[11] = new ParamField("@PRE9", "PRE9");
                            pars[12] = new ParamField("@PRICE", "PRICE");
                            pars[13] = new ParamField("@PYZJM", "PYZJM");
                            pars[14] = new ParamField("@REMARK", "REMARK");
                            pars[15] = new ParamField("@SFLB", "SFLB");
                            pars[16] = new ParamField("@UNIT", "UNIT");
                            pars[17] = new ParamField("@WBZJM", "WBZJM");
                            pars[18] = new ParamField("@YPDJ", "YPDJ");
                            pars[19] = new ParamField("@YPMC", "YPMC");
                            pars[20] = new ParamField("@YPBM", "YPBM");
                            sqlStruct.ParamFields = pars;
                            data.ExecuteNonQuery(row, sqlStruct);
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        public void deleteEntity(DataSet entityData)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                ParamField[] pars;
                sqlStruct.SqlString = "DELETE YB_YP_INFO WHERE YPBM=@YPBM";
                pars = new ParamField[1];
                pars[0] = new ParamField("@YPBM", "YPBM");
                sqlStruct.ParamFields = pars;
                foreach (DataRow row in entityData.Tables["YB_YP_INFO"].Rows)
                {
                    data.ExecuteNonQuery(row, sqlStruct);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        public DataSet FindByPrimaryKey(object strKeyValue)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                string sql = "";
                QueryParameterCollection para;
                YBYPInfoData ds = new YBYPInfoData();
                sql = "SELECT YB_YP_INFO.GG,YB_YP_INFO.JX,YB_YP_INFO.PRE0,YB_YP_INFO.PRE1,YB_YP_INFO.PRE2,YB_YP_INFO.PRE3,YB_YP_INFO.PRE4,YB_YP_INFO.PRE5,YB_YP_INFO.PRE6,YB_YP_INFO.PRE7,YB_YP_INFO.PRE8,YB_YP_INFO.PRE9,YB_YP_INFO.PRICE,YB_YP_INFO.PYZJM,YB_YP_INFO.REMARK,YB_YP_INFO.SFLB,YB_YP_INFO.UNIT,YB_YP_INFO.WBZJM,YB_YP_INFO.YPBM,YB_YP_INFO.YPDJ,YB_YP_INFO.YPMC from YB_YP_INFO WHERE YPBM=@YPBM";
                para = new QueryParameterCollection(1);
                para.Add("@YPBM", strKeyValue);
                data.ExecuteDataset(sql, para, ds, "YB_YP_INFO");
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        #endregion
        public DataSet FindByPrimaryKey(ArrayList KeyValue)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                string sql = "";
                QueryParameterCollection para;
                YBYPInfoData ds = new YBYPInfoData();
                sql = "SELECT YB_YP_INFO.GG,YB_YP_INFO.JX,YB_YP_INFO.PRE0,YB_YP_INFO.PRE1,YB_YP_INFO.PRE2,YB_YP_INFO.PRE3,YB_YP_INFO.PRE4,YB_YP_INFO.PRE5,YB_YP_INFO.PRE6,YB_YP_INFO.PRE7,YB_YP_INFO.PRE8,YB_YP_INFO.PRE9,YB_YP_INFO.PRICE,YB_YP_INFO.PYZJM,YB_YP_INFO.REMARK,YB_YP_INFO.SFLB,YB_YP_INFO.UNIT,YB_YP_INFO.WBZJM,YB_YP_INFO.YPBM,YB_YP_INFO.YPDJ,YB_YP_INFO.YPMC from YB_YP_INFO WHERE YPBM=@YPBM";
                para = new QueryParameterCollection(1);
                para.Add("@YPBM", KeyValue[0]);
                data.ExecuteDataset(sql, para, ds, "YB_YP_INFO");
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        /// <summary>
        /// 删除实体记录
        /// </summary>
        public void deleteEntity()
        {
            try
            {
                string SqlString = "DELETE FROM YB_YP_INFO ";
                data.ExecuteNonQuery(SqlString);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        /// <summary>
        /// 查找所有药品目录信息
        /// </summary>
        /// <returns></returns>
        public DataSet FindAll()
        {
            try
            {
                string sql = "SELECT YB_YP_INFO.* FROM YB_YP_INFO ";
                DataSet ds = new DataSet();
                data.ExecuteDataset(sql, ds);
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
    }
}
